require "warningCfg"

--parent should has bt callback function -- warningPanelCallback(type, btBeenHitIdx)
--btIdx == 0 standfor "close"
WarningPanel = class("WarningPanel")

function WarningPanel.create(parent, typekey, zorder)
    local panel = WarningPanel.new()
    panel.typekey = typekey
    panel:init(parent)

    uiEffect.bounceIn(panel.view)
    panel.view:setPosition(pp.center.x, pp.center.y+20)
    if zorder then
        panel.view:setLocalZOrder(zorder)   --用户自定义zorder;否则在init里面设置为pp.z_warning
    end
    parent:base_onOpenWin(panel)
    return panel
end


function WarningPanel:init(parent)
    self.view = ccs.GUIReader:getInstance():widgetFromJsonFile(warningCfg[self.typekey].json)
    parent.widget:addChild(self.view)
    self.view:setLocalZOrder(pp.z_warning)
    self.parent = parent

    local function btCloseCallback(sender, eventType)
        if eventType == ccui.TouchEventType.began then
            AudioEngine.playEffect(sound.btn2)
        elseif eventType == ccui.TouchEventType.ended and not self.bAlreadyClicked then
            parent:warningPanelCallback(self.typekey, 0, self)
            self:close()
        end
    end
    self.bt_close = self.view:getChildByName("_close")
    if self.bt_close then
        self.bt_close:addTouchEventListener(btCloseCallback)
    end

    --bts
    self.bt_1_1 = self.view:getChildByName("_bt_1_1")
    self.bt_1_1:setVisible(false)

    self.bt_2_1 = self.view:getChildByName("_bt_2_1")
    self.bt_2_1:setVisible(false)

    self.bt_2_2 = self.view:getChildByName("_bt_2_2")
    self.bt_2_2:setVisible(false)


    local typeinfo = warningCfg[self.typekey]

    --fnt by Cfg
    self.fnt = self.view:getChildByName("_fnt")
    self.fnt:loadTexture(typeinfo.fntImg, ccui.TextureResType.localType)

    --countdown by Cfg
    if typeinfo.countdown then

        self.timeleft = typeinfo.countdown.time

        self.timefnt = cc.LabelBMFont:create("", typeinfo.countdown.fnt)
        self.timefnt:setPosition(typeinfo.countdown.pos.x, typeinfo.countdown.pos.y)
        self.timefnt:setScale(typeinfo.countdown.scale)
        self.view:addChild(self.timefnt, 5)

        local function update(delta)
           self:update(delta)
        end
        self.view:scheduleUpdateWithPriorityLua(update, 0)

    end

    --bt by Cfg
    local btsInfo = typeinfo.bts
    
    -------------------------------------
    --specail
    if gameData.SwichByServer.LicenseMode then
        if btsInfo[1].img_license then
            btsInfo[1].img = typeinfo.bts[1].img_license
        end
    end
    -------------------------------------
    
    if #btsInfo == 1 then
        self.bt_1_1:setVisible(true)
        self.bt_1_1:loadTextureNormal(btsInfo[1].img, ccui.TextureResType.localType)

        local function callback(sender, eventType)
            if eventType == ccui.TouchEventType.began then
                AudioEngine.playEffect(sound.btn1)
            elseif eventType == ccui.TouchEventType.ended and not self.bAlreadyClicked then
                parent:warningPanelCallback(self.typekey, 1, self)
                self.bAlreadyClicked = true
            end
        end
        self.bt_1_1:addTouchEventListener(callback)
        if btsInfo[1].ani then uiEffect.jelly(self.bt_1_1) end

    elseif #btsInfo == 2 then
        self.bt_2_1:setVisible(true)
        self.bt_2_1:loadTextureNormal(btsInfo[1].img, ccui.TextureResType.localType)
        local function callback1(sender, eventType)
            if eventType == ccui.TouchEventType.began then
                AudioEngine.playEffect(sound.btn1)
            elseif eventType == ccui.TouchEventType.ended and not self.bAlreadyClicked then
                parent:warningPanelCallback(self.typekey, 1, self)
                self.bAlreadyClicked = true
            end
        end
        self.bt_2_1:addTouchEventListener(callback1)
        if btsInfo[1].ani then uiEffect.jelly(self.bt_2_1) end

        self.bt_2_2:setVisible(true)
        self.bt_2_2:loadTextureNormal(btsInfo[2].img, ccui.TextureResType.localType)
        local function callback2(sender, eventType)
            if eventType == ccui.TouchEventType.began then
                AudioEngine.playEffect(sound.btn1)
            elseif eventType == ccui.TouchEventType.ended and not self.bAlreadyClicked then
                parent:warningPanelCallback(self.typekey, 2, self)
                self.bAlreadyClicked = true
            end
        end
        self.bt_2_2:addTouchEventListener(callback2)
        if btsInfo[2].ani then uiEffect.jelly(self.bt_2_2) end

    end
end

function WarningPanel:close()
    -- body
    self.view:unscheduleUpdate()
    
    uiEffect.bounceOut(self.view, true)
    self.parent:base_onCloseWin(self)
end

function WarningPanel:update(delta)
    -- body
    self.timeleft = self.timeleft - delta
    if self.timeleft <= 0 then
        self.view:unscheduleUpdate()
        
        self.parent:warningPanelCallback(self.typekey, 3)
        self:close()

    else
        local showtime = math.floor(self.timeleft)
        self.timefnt:setString(string.format("%d", showtime))
    end
end







